package com.example.casper.Experiment2024;


import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.longClick;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static com.example.casper.Experiment2024.Test.ImageUtils.areImagesSame;
import static com.example.casper.Experiment2024.Test.ImageUtils.getCoverBitmap;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class Hello2414ActivityTest {

    @Rule
    public ActivityScenarioRule<Hello2414Activity> mActivityScenarioRule =
            new ActivityScenarioRule<>(Hello2414Activity.class);

    @Test
    public void hello2414ActivityTest() {
        ViewInteraction recyclerView = onView(
                allOf(withId(R.id.recycler_view_the_books),
                        childAtPosition(
                                withId(R.id.main),
                                0)));
        recyclerView.perform(actionOnItemAtPosition(1, longClick()));

        ViewInteraction materialTextView = onView(
                allOf(withId(android.R.id.title), withText("修改1"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        materialTextView.perform(click());

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.titleText), withText("软件项目管理案例教程（第3版）"),
                        childAtPosition(
                                allOf(withId(R.id.main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                0),
                        isDisplayed()));
        appCompatEditText.perform(replaceText("高等数学"));

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.titleText), withText("高等数学"),
                        childAtPosition(
                                allOf(withId(R.id.main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                0),
                        isDisplayed()));
        appCompatEditText2.perform(closeSoftKeyboard());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.coverResourceIdText), withText("res/drawable/book_2.jpg"),
                        childAtPosition(
                                allOf(withId(R.id.main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                1),
                        isDisplayed()));
        appCompatEditText3.perform(replaceText(""));

        ViewInteraction appCompatEditText4 = onView(
                allOf(withId(R.id.coverResourceIdText),
                        childAtPosition(
                                allOf(withId(R.id.main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                1),
                        isDisplayed()));
        appCompatEditText4.perform(closeSoftKeyboard());

        ViewInteraction materialButton = onView(
                allOf(withId(R.id.saveButton), withText("保存"),
                        childAtPosition(
                                allOf(withId(R.id.main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                2),
                        isDisplayed()));
        materialButton.perform(click());

        ViewInteraction textView = onView(
                allOf(withId(R.id.text_view_book_name), withText("高等数学"),
                        withParent(withParent(withId(R.id.recycler_view_the_books))),
                        isDisplayed()));
        textView.check(matches(withText("高等数学")));

        onView(withId(R.id.recycler_view_the_books))
                .perform(actionOnItemAtPosition(1, //指的需要被测试图片的位置
                        new ViewAction() {
                            @Override
                            public Matcher<View> getConstraints() {
                                return isAssignableFrom(ImageView.class);
                            }
                            @Override
                            public String getDescription() {
                                return "Check image in item at position 0";
                            }
                            @Override
                            public void perform(UiController uiController, View view) {
                                Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
                                ImageView imageView = view.findViewById(R.id.image_view_book_cover);
                                Bitmap actualBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
                                assertTrue(areImagesSame(getCoverBitmap(R.drawable.book_no_name,appContext), actualBitmap));
                                //选择预期的图片
                            }
                        }
                ));
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}
